﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Expression.Shapes;

namespace Lottery.Client.SL.Controls
{
    public partial class News : UserControl
    {
        private bool iUT8DhDs8W;

        public News()
        {
            InitializeComponent();
            Loaded += News_Loaded;
            this.CvComeback.MouseLeftButtonDown += this.CvComeback_MouseLeftButtonDown;
        }

        private void News_Loaded(object obj, RoutedEventArgs routedEventArgs)
        {
            SetupControls();
        }

        private void SetupControls()
        {
            NewSPanel.Children.Clear();
            //官方公告列表
            foreach (NewsData t in DataCache.NewsDatas)
            {
                //公告元素
                var newsHeader = new Canvas();
                newsHeader.MouseLeftButtonDown += (LoadNewsDetailPanel);
                //鼠标hover特效
                newsHeader.MouseEnter += (NewsHeader_MouseIn);
                newsHeader.MouseLeave += (NewsHeader_Mouseout);
                newsHeader.Height = 35.0;
                newsHeader.Width = 690.0;

                // 0 
                /*<Color x:Key="NewSelectRectBackColor">#7F869593</Color>
        <Color x:Key="NoNewTemplateRectColor">#7F353839</Color>
        <Color x:Key="NoNewTemplateRectStrokeColor">#0CFFFFFF</Color>*/
                var rectangle = new Rectangle {Height = 35.0, Width = 690.0, StrokeThickness = 1.0};
                rectangle.Fill = new SolidColorBrush((Color)base.Resources["NoNewTemplateRectColor"]);// .get_Item(BcZJxKMIDcH0kvSFls.w4WekCpilj(7782))));
                rectangle.Stroke = new SolidColorBrush((Color)base.Resources["NoNewTemplateRectStrokeColor"]);
                rectangle.Stroke = new SolidColorBrush((Color)base.Resources["NoNewTemplateRectStrokeColor"]);
                newsHeader.Children.Add(rectangle);
                // 1
                var textBlock = new TextBlock();
                textBlock.Tag = t.ID;
                textBlock.Text = t.AffichesTitle;
                textBlock.Style = ((Style)base.Resources["NewTemplateTextStyle"]);
                textBlock.SetValue(Canvas.TopProperty, 9.0);
                textBlock.SetValue(Canvas.LeftProperty, 38.0);
                newsHeader.Children.Add(textBlock);
                // 2
                var regularPolygon = new RegularPolygon();
                regularPolygon.Style = ((Style)base.Resources["NewRegularPolygonStyle1"]); ;
                regularPolygon.SetValue(Canvas.TopProperty, 16.0);
                regularPolygon.SetValue(Canvas.LeftProperty, 14.0);
                regularPolygon.Stretch = Stretch.Fill;
                newsHeader.Children.Add(regularPolygon);
                newsHeader.Margin = new Thickness(0.0, -1.0, 0.0, 0.0);
                NewSPanel.Children.Add(newsHeader);
            }
        }

        private void NewsHeader_Mouseout(object obj, MouseEventArgs mouseEventArgs)
        {
            var canvas = (Canvas) obj;
            var rectangle = (Rectangle) canvas.Children[0];
            rectangle.Fill = new SolidColorBrush((Color)(base.Resources["NoNewTemplateRectColor"]));
        }

        private void NewsHeader_MouseIn(object obj, MouseEventArgs mouseEventArgs)
        {
            var canvas = (Canvas) obj;
            var rectangle = (Rectangle) canvas.Children[0];
            rectangle.Fill = new SolidColorBrush((Color)(base.Resources["NewSelectRectBackColor"]));
        }

        private void CvComeback_MouseLeftButtonDown(object obj, MouseButtonEventArgs mouseButtonEventArgs)
        {
            CvNewList.Visibility = Visibility.Visible;
            CvNewsCount.Visibility = Visibility.Collapsed;
            tbNewsTitle.Text = "";
            rnNewsCount.Text = "";
        }

        private void LoadNewsDetailPanel(object obj, MouseButtonEventArgs mouseButtonEventArgs)
        {
            if (obj != null)
            {
                var canvas = (Canvas) obj;
                if (canvas.Children.Count == 3)
                {
                    //公告列表
                    CvNewList.Visibility = Visibility.Collapsed;
                    //公告详细信息面板
                    CvNewsCount.Visibility = Visibility.Visible;
                    var textBlock = (TextBlock) canvas.Children[1];
                    NewsData current = DataCache.NewsDatas.First(v => v.ID.ToString() == textBlock.Tag.ToString());
                    tbNewsTitle.Text = current.AffichesTitle + " (" +
                                       DateTime.Parse(current.AffichesTime).ToShortDateString() +
                                       ")";
                    rnNewsCount.Text = current.AffichesCount;
                }
            }
        }
    }
}